namespace MockingBird.Simulator.Framework.Configuration
{
    using System.Collections.Generic;
    using System.Configuration;
    using System.IO;
    using System.Xml;
    using MockingBird.Common.Infrastructure.Configuration;

    public class NativeConfigurationDataProvider : IConfigurationDataProvider
    {
        private readonly string configFilesRoot;
        private readonly string handlerInfoFile;
        private Dictionary<string, string> settingsDictionary;
        private readonly HandlerMapManager handlerMapManager;
        private readonly FolderManager folderManager;

        private const string NativeAppSettingsKeyName = "mockingbird_configuration_folder";
        private const string MockingBirdConfigFileName = "mockingbird.config";
        private const string HandlerInfoFileName = "handlerMap.config";
        private const string RootConfigSectionName = "Global";
        private const string SettingElementName = "Setting";
        private const string KeyAttributeName = "name";
        private const string ValueAttributeName = "value";

        public NativeConfigurationDataProvider()
        {
            this.configFilesRoot = ConfigurationManager.AppSettings[NativeAppSettingsKeyName];
            this.settingsDictionary = new Dictionary<string, string>();
            this.handlerInfoFile = Path.Combine(this.configFilesRoot, HandlerInfoFileName);
            this.handlerMapManager = new HandlerMapManager(handlerInfoFile);
            this.folderManager = new FolderManager();
            this.folderManager.InitDataFilesRoot(this.configFilesRoot);
        }
        #region IConfigurationDataProvider Members

        public string ConfigurationFilesRoot
        {
            get
            {
                return this.configFilesRoot;
            }
        }


        public Dictionary<string, string> GetGlobalSettings()
        {
            string configFile = Path.Combine(this.configFilesRoot, MockingBirdConfigFileName);
            string configSectionXml = ConfigurationUtils.GetConfigSectionXml(configFile, RootConfigSectionName);
            var configDoc = new XmlDocument();
            configDoc.LoadXml(configSectionXml);
            this.settingsDictionary = ConfigurationUtils.LoadDictionaryFromNameValueSettings(configDoc, SettingElementName, KeyAttributeName, ValueAttributeName);
            return settingsDictionary;
        }

        public HandlerRegistrationInfo GetHandlerRegistrationInfo()
        {
            return handlerMapManager.HandlerRegistrations;
        }

        public HandlerMapInfo GetHandlerMapInfo()
        {
            return handlerMapManager.HandlerMap;
        }

        public void AddHandlerMapEntry(HandlerMapEntry mapEntry)
        {
            this.handlerMapManager.AddHandlerMapEntry(mapEntry);

            this.handlerMapManager.SaveConfiguration();
        }

        public List<string> GetRegisteredEndpoints()
        {
            return this.handlerMapManager.GetRegisteredEndpoints();
        }

        public string GetConfigFile(string endpointName)
        {
            return this.folderManager.GetConfigFile(endpointName);
        }

        public string GetConfigFile(string endpointName, string basePath)
        {
            this.folderManager.InitDataFilesRoot(basePath);
            return this.folderManager.GetConfigFile(endpointName);
        }

        public string GetRequestRoot(string endpointName)
        {
            return this.folderManager.GetRequestRoot(endpointName);
        }

        public string GetResponseRoot(string endpointName)
        {
            return this.folderManager.GetResponseRoot(endpointName);
        }

        #endregion
    }
}